<template>
  <div class="container animate__animated animate__fadeIn">
    <img
      v-if="myFosterageList.length == 0"
      src="../svg/no-data.svg"
      width="70%"
      style="display: block; margin: 0 auto"
    />
    <ul>
      <li
        v-for="item in myFosterageList"
        :key="item.id"
        class="animate__animated animate__fadeIn"
      >
        <div class="picture">
          <img :src="item.picture" alt="" width="231px" height="130px" />
        </div>
        <div class="content">
          <div style="width: 380px; height: 100%; padding-top: 20px">
            <div
              style="
                width: 100%;
                height: 35px;
                font-size: 16px;
                color: #000;
                font-weight: 600;
              "
            >
              <span>标题</span>&nbsp;&nbsp;
              <span
                >
                <a-tag color="orange" v-if="item.petType == '猫'"> {{ item.petType }} </a-tag>
                <a-tag color="cyan" v-else-if="item.petType == '狗'"> {{ item.petType }} </a-tag>
                <a-tag color="purple" v-else-if="item.petType == '兔'"> {{ item.petType }} </a-tag>
                <a-tag v-else> {{ item.petType }} </a-tag>
                </span
              >&nbsp;&nbsp;
              <span>
                <a-tag color="blue" v-if="item.petSex == 1"> 雄性 </a-tag>
                <a-tag color="pink" v-else-if="item.petSex == 2"> 雌性 </a-tag>
                <a-tag v-else> 未知性别 </a-tag> </span
              >&nbsp;&nbsp;
              <span
                ><a-tag color="green"> {{ item.petMonth }}个月 </a-tag></span
              >
            </div>
            <div class="title-content">
              {{ item.title }}
            </div>
            <div
              style="
                width: 100%;
                height: 24px;
                color: rgb(140, 140, 140);
                overflow: hidden;
              "
            >
              <span
                style="
                  float: right;
                  font-size: 13px;
                  font-weight: 200;
                  color: rgb(130, 130, 130);
                "
              >
                {{ item.createTime }}
              </span>
            </div>
          </div>
        </div>
        <div class="operation">
          <a-button
            class="primary-btn"
            type="primary"
            ghost
            @click="viewDetail(item.id)"
          >
            详情
          </a-button>
          <a-popconfirm
            placement="leftBottom"
            title="确定要删除吗？"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDelete(item.id)"
          >
            <a-button
              class="danger-btn"
              type="danger"
              ghost
              style="margin-top: 10px"
            >
              删除
            </a-button>
          </a-popconfirm>
        </div>
      </li>
    </ul>
    <div class="page-container" v-if="myFosterageList.length != 0">
      <a-pagination
        :defaultPageSize="pageInfo.pageSize"
        :total="pageInfo.total"
        style="display: flex; justify-content: end"
        @change="handlePageChange"
      />
    </div>
  </div>
</template>

<script>
import requests from "../js/requests.js";
import { Pagination, Button, Tag, Popconfirm } from "ant-design-vue";

export default {
  name: "MyFosterage",
  components: {
    APagination: Pagination,
    AButton: Button,
    ATag: Tag,
    APopconfirm: Popconfirm,
  },
  data() {
    return {
      pageInfo: {
        pageNum: 1,
        pageSize: 5,
        total: 0,
      },
      myFosterageList: [],
    };
  },
  mounted() {
    requests
      .getMyFosterage(this.pageInfo.pageNum, this.pageInfo.pageSize)
      .then((res) => {
        if (res.code == 200) {
          this.pageInfo.total = res.data.total;
          this.myFosterageList = res.data.list;
        } else {
          this.$message.error(res.msg);
        }
      });
  },
  methods: {
    handlePageChange(page, pageSize) {
      requests.getMyFosterage(page, pageSize).then((res) => {
        if (res.code == 200) {
          this.pageInfo.pageNum = res.data.pageNum;
          this.pageInfo.total = res.data.total;
          this.myFosterageList = res.data.list;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    viewDetail(id) {
      this.$router.push({ path: "/detail", query: { id } });
    },
    handleDelete(id) {
      requests.deleteBailmentData(id).then((res) => {
        if (res.code == 200) {
          this.$message.success(res.msg);
          requests
            .getMyFosterage(this.pageInfo.pageNum, this.pageInfo.pageSize)
            .then((res) => {
              if (res.code == 200) {
                this.pageInfo.pageNum = res.data.pageNum;
                this.pageInfo.total = res.data.total;
                this.myFosterageList = res.data.list;
              } else {
                this.$message.error(res.msg);
              }
            });
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
.container {
  width: 100%;
  height: 100%;
}

.container ul {
  width: 95%;
  margin: 0 auto;
}

.container ul li {
  width: 100%;
  height: 150px;
  margin-top: 7px;
  display: flex;
  justify-content: space-between;

  border-radius: 10px;
  background-color: rgb(255, 255, 255);
  transition: 0.5s;
}

.container ul li:hover {
  box-shadow: 0 2px 10px rgb(0, 0, 0, 0.8);
}

.picture {
  width: 251px;
  border-radius: 10px 0px 30px 10px;
  background-color: rgb(225, 225, 225);
}

.picture img {
  margin-top: 10px;
  margin-left: 10px;
  border-radius: 10px;
}
.content {
  width: 350px;
  /* background-color: red; */
}

.title-content {
  width: 100%;
  height: 62px;
  color: rgb(120, 120, 120);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.operation {
  width: 120px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  /* background-color: rgb(101, 53, 53); */
}

.primary-btn:hover {
  box-shadow: 0px 1px 3px 0px rgb(25, 144, 255);
  transform: translate(0, -1px);
}

.danger-btn:hover {
  box-shadow: 0px 1px 3px 0px rgb(255, 78, 80);
  transform: translate(0, -1px);
}

.page-container {
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: end;
  align-items: center;
  /* background-color: antiquewhite; */
}
</style>